package com.tmc.util;

import java.text.ParseException;
import java.util.Date;

import com.tmc.bean.intair.EthFare;
import com.tmc.bean.intair.EthFlight;
import com.tmc.constant.OrderConstant;
import com.tmc.constant.PolicyConstant;
import com.tmc.entity.intair.CommisionPolicy;
import com.tmc.entity.intair.IntairFlight;
import com.tmc.entity.intair.IntairOrder;
import com.tmc.entity.intair.PrivateFare;
import com.tmc.entity.intair.TripartitePolicy;

public class PolicyMatchUtil {
	// 以下匹配政策
	public static boolean isAirlineMatch(String ticketingAirline, String policyAirline, String policyAirlineShare) {
		if (policyAirline == null || "".equals(policyAirline))
			return true;
		if (policyAirline.equalsIgnoreCase(ticketingAirline))
			return true;
		// 共享航司
		if (ObjectUtil.hasText(policyAirlineShare) && policyAirlineShare.contains(ticketingAirline)) {
			return true;
		}
		return false;
	}

	public static boolean isCabinMatch(EthFlight flight, String policyDepartureCabins, String policyTransferCabins) {
		if (ObjectUtil.hasText(flight.getTransfer())) {
			// 中转
			for (int i = 0; i < flight.getSegments().size(); i++) {
				String carbin = flight.getSegments().get(i).getBookingCode();
				if (i == 0) {
					if (ObjectUtil.hasText(policyDepartureCabins) && !policyDepartureCabins.contains(carbin + "/")) {
						return false;
					}
				} else {
					if (ObjectUtil.hasText(policyTransferCabins) && !policyTransferCabins.contains(carbin + "/")) {
						return false;
					}
				}
			}
		} else {// 直飞
			String cabin = flight.getSegments().get(0).getBookingCode();
			if (ObjectUtil.hasText(policyDepartureCabins) && !policyDepartureCabins.contains(cabin + "/")) {
				return false;
			}
		}
		return true;
	}
	
	public static boolean isCabinMatch(IntairFlight flight, String policyDepartureCabins, String policyTransferCabins) {
		if (ObjectUtil.hasText(flight.getTransfer())) {
			// 中转
			for (int i = 0; i < flight.getSegments().size(); i++) {
				String carbin = flight.getSegments().get(i).getBookingCode();
				if (i == 0) {
					if (ObjectUtil.hasText(policyDepartureCabins) && !policyDepartureCabins.contains(carbin + "/")) {
						return false;
					}
				} else {
					if (ObjectUtil.hasText(policyTransferCabins) && !policyTransferCabins.contains(carbin + "/")) {
						return false;
					}
				}
			}
		} else {// 直飞
			String cabin = flight.getSegments().get(0).getBookingCode();
			if (ObjectUtil.hasText(policyDepartureCabins) && !policyDepartureCabins.contains(cabin + "/")) {
				return false;
			}
		}
		return true;
	}

	public static boolean isDateMatch(CommisionPolicy policy, EthFare fare, EthFlight flight) {
		if (!isTicketingDateMatch(policy.getTicketingDateEnd(), policy.getTicketingAdvance())) {
			return false;
		}
		if (!isDateMatch(fare, flight, policy.getOutboundTravelDateStart(), policy.getOutboundTravelDateEnd(),
				policy.getOutboundTravelDateExcludeStart(), policy.getOutboundTravelDateExcludeEnd(),
				policy.getInboundTravelDateStart(), policy.getInboundTravelDateEnd(),
				policy.getInboundTravelDateExcludeStart(), policy.getInboundTravelDateExcludeEnd())) {
			return false;
		}
		return true;
	}

	public static boolean isDateMatch(CommisionPolicy policy, IntairOrder order, IntairFlight flight) {
		if (!isTicketingDateMatch(policy.getTicketingDateEnd(), policy.getTicketingAdvance())) {
			return false;
		}
		if (!isDateMatch(order, flight, policy.getOutboundTravelDateStart(), policy.getOutboundTravelDateEnd(),
				policy.getOutboundTravelDateExcludeStart(), policy.getOutboundTravelDateExcludeEnd(),
				policy.getInboundTravelDateStart(), policy.getInboundTravelDateEnd(),
				policy.getInboundTravelDateExcludeStart(), policy.getInboundTravelDateExcludeEnd())) {
			return false;
		}
		return true;
	}

	public static boolean isDateMatch(PrivateFare policy, EthFare fare, EthFlight flight) {
		if (!isTicketingDateMatch(policy.getTicketingDateEnd(), policy.getTicketingAdvance())) {
			return false;
		}
		if (!isDateMatch(fare, flight, policy.getOutboundTravelDateStart(), policy.getOutboundTravelDateEnd(),
				policy.getOutboundTravelDateExcludeStart(), policy.getOutboundTravelDateExcludeEnd(),
				policy.getInboundTravelDateStart(), policy.getInboundTravelDateEnd(),
				policy.getInboundTravelDateExcludeStart(), policy.getInboundTravelDateExcludeEnd())) {
			return false;
		}
		return true;
	}

	public static boolean isDateMatch(PrivateFare policy, IntairOrder order, IntairFlight flight) {
		if (!isTicketingDateMatch(policy.getTicketingDateEnd(), policy.getTicketingAdvance())) {
			return false;
		}
		if (!isDateMatch(order, flight, policy.getOutboundTravelDateStart(), policy.getOutboundTravelDateEnd(),
				policy.getOutboundTravelDateExcludeStart(), policy.getOutboundTravelDateExcludeEnd(),
				policy.getInboundTravelDateStart(), policy.getInboundTravelDateEnd(),
				policy.getInboundTravelDateExcludeStart(), policy.getInboundTravelDateExcludeEnd())) {
			return false;
		}
		return true;
	}
	
	private static boolean isDateMatch(IntairOrder order, IntairFlight flight, Long outboundTravelDateStart,
			Long outboundTravelDateEnd, Long outboundTravelDateExcludeStart, Long outboundTravelDateExcludeEnd,
			Long inboundTravelDateStart, Long inboundTravelDateEnd, Long inboundTravelDateExcludeStart,
			Long inboundTravelDateExcludeEnd) {
		if (flight == order.getFlights().get(0)) {
			// 去程适用日期
			if (!isBefore(outboundTravelDateStart,
					DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
 				return false;
			}
			if (!isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd), outboundTravelDateEnd + DateUtil.Day2Long)) {
				return false;
			}
			// 去程不适用日期
			if (outboundTravelDateExcludeStart != null && outboundTravelDateExcludeStart != 0
					&& isBefore(outboundTravelDateExcludeStart,
							DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
				return false;
			}
			if (outboundTravelDateExcludeEnd != null && outboundTravelDateExcludeEnd != 0
					&& isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd),
							outboundTravelDateExcludeEnd + DateUtil.Day2Long)) {
				return false;
			}

		} else {// 返程日期
			if (OrderConstant.FLIGHT_TYPE_RT.equals(order.getFlightType()) && !isBefore(inboundTravelDateStart,
					DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
				return false;
			}
			if (OrderConstant.FLIGHT_TYPE_RT.equals(order.getFlightType())
					&& !isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd),
							inboundTravelDateEnd + DateUtil.Day2Long)) {
				return false;
			}
		}
		return true;
	}

	private static boolean isDateMatch(EthFare fare, EthFlight flight, Long outboundTravelDateStart,
			Long outboundTravelDateEnd, Long outboundTravelDateExcludeStart, Long outboundTravelDateExcludeEnd,
			Long inboundTravelDateStart, Long inboundTravelDateEnd, Long inboundTravelDateExcludeStart,
			Long inboundTravelDateExcludeEnd) {
		if (flight == fare.getFlights().get(0)) {
			// 去程适用日期
			if (!isBefore(outboundTravelDateStart,
					DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
				return false;
			}
			if (!isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd),
					outboundTravelDateEnd + DateUtil.Day2Long)) {
				return false;
			}
			// 去程不适用日期
			if (outboundTravelDateExcludeStart != null && outboundTravelDateExcludeStart != 0
					&& isBefore(outboundTravelDateExcludeStart,
							DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
				return false;
			}
			if (outboundTravelDateExcludeEnd != null && outboundTravelDateExcludeEnd != 0
					&& isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd),
							outboundTravelDateExcludeEnd + DateUtil.Day2Long)) {
				return false;
			}

		} else {// 返程日期
			if ("RT".equals(fare.getFlightType()) && !isBefore(inboundTravelDateStart,
					DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
				return false;
			}
			if ("RT".equals(fare.getFlightType())
					&& !isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd),
							inboundTravelDateEnd + DateUtil.Day2Long)) {
				return false;
			}
		}
		return true;
	}
	
	public static boolean isDateMatch(TripartitePolicy policy, IntairOrder order, IntairFlight flight){
		if (!isTripartiteDateMatch(order, flight, policy.getFlightDateStart(), policy.getFlightDateStart(),
				policy.getFlightDateExcludeStart(), policy.getFlightDateExcludeEnd(), policy.getProtocolDateStart(),
				policy.getProtocolDateEnd(), policy.getTicketingDateStart(), policy.getTicketingDateEnd())) {
			return false;
		}
		return true;
	}
	
	private static boolean isTripartiteDateMatch(IntairOrder order, IntairFlight flight, Long flightDateStart,
			Long flightDateEnd, Long flightDateExcludeStart, Long flightDateExcludeEnd, Long protocolDateStart,
			Long protocolDateEnd, Long ticketingDateStart, Long ticketingDateEnd) {

		// 乘机适用日期
		if (!isBefore(flightDateStart, DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
			return false;
		}
		if (!isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd),
				flightDateEnd + DateUtil.Day2Long)) {
			return false;
		}
		// 乘机不适用日期
		if (flightDateExcludeStart != null && flightDateExcludeStart != 0 && isBefore(flightDateExcludeStart,
				DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
			return false;
		}
		if (flightDateExcludeEnd != null && flightDateExcludeEnd != 0
				&& isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd),
						flightDateExcludeEnd + DateUtil.Day2Long)) {
			return false;
		}
		
		// 协议有效期
		if (!isBefore(protocolDateStart, DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd))) {
			return false;
		}
		if (!isBefore(DateUtil.toLongDate(flight.getDepartureDate(), DateUtil.yyyy_MM_dd),
				protocolDateEnd + DateUtil.Day2Long)) {
			return false;
		}

		// 订票有效期
		if (!isBefore(ticketingDateStart, order.getBookDate())) {
			return false;
		}
		if (!isBefore(order.getBookDate(), ticketingDateEnd + DateUtil.Day2Long)) {
			return false;
		}
		return true;
	}

	public static boolean isTicketingDateMatch(Long ticketingDateEnd, Integer ticketingAdvance) {
		// 判断提前开票天数是否符合条件
		if (ticketingDateEnd != null && ticketingAdvance != null) {
			if (!isBefore(
					DateUtil.toLongDate(DateUtil.addDate(new Date(), ticketingAdvance - 1)),
					ticketingDateEnd)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isBefore(Long date1, Long date2) {
		if (date1 == null || date1 == 0 || date2 == null || date2 == 0) {
			return false;
		}
		try {
			if (DateUtil.isBefore(DateUtil.toStrDate(date1, DateUtil.yyyy_MM_dd),
					DateUtil.toStrDate(date2, DateUtil.yyyy_MM_dd))) {
				return true;
			}
		} catch (ParseException e) {
			// logger.error("时间转换出错!", e);
		}
		return false;
	}

	public static boolean isTransferMatch(String transferCode, String countryCode, String policyTransfer,
			String policyTransferExclude) {
		
		if (ObjectUtil.noText(transferCode)) {
			return true;
		}
		
		String airportCode = transferCode;
//		if (ObjectUtil.hasText(policyTransfer) && !(policyTransfer.contains(transferArea + "/")
//				|| policyTransfer.contains(countryCode + "/") || policyTransfer.contains(airportCode + "/"))) {
//			return false;
//		}
		if(ObjectUtil.hasText(policyTransfer)){
			String transferCountrys = PolicyConstant.getCountrys(policyTransfer.substring(0, policyTransfer.length() - 1));
			if (!((ObjectUtil.hasText(transferCountrys) && transferCountrys.contains(countryCode + "/")) || policyTransfer.contains(countryCode + "/")
					|| policyTransfer.contains(airportCode + "/"))) {

			}
		}
		if (ObjectUtil.hasText(policyTransferExclude) && (policyTransferExclude.contains(countryCode + "/")
				|| policyTransferExclude.contains(airportCode + "/"))) {
			return false;
		}

		return true;
	}

	public static boolean isDirectMatch(String departure, String depCountryCode, String arrival, String ariCountryCode,
			CommisionPolicy policy) {
		String depAirportCode = departure;
		String ariAirportCode = arrival;
//		String depArea = PolicyConstant.getArea(depCountryCode);
//		String ariArea = PolicyConstant.getArea(depCountryCode);
//		if (ObjectUtil.hasText(policy.getDeparture()) && !(policy.getDeparture().contains(depArea + "/")
//				|| policy.getDeparture().contains(depCountryCode + "/")
//				|| policy.getDeparture().contains(depAirportCode + "/"))) {
//			return false;
//		}
		if (ObjectUtil.hasText(policy.getDeparture())) {
			String depCountrys = PolicyConstant
					.getCountrys(policy.getDeparture().substring(0, policy.getDeparture().length() - 1));
			if (!((ObjectUtil.hasText(depCountrys) && depCountrys.contains(depCountryCode + "/")) || policy.getDeparture().contains(depCountryCode + "/")
					|| policy.getDeparture().contains(depAirportCode + "/"))) {
				return false;
			}
		}
		if (ObjectUtil.hasText(policy.getDepartureExclude())
				&& (policy.getDepartureExclude().contains(depCountryCode + "/")
						|| policy.getDepartureExclude().contains(depAirportCode + "/"))) {
			return false;
		}
//		if (ObjectUtil.hasText(policy.getArrival())
//				&& !(policy.getArrival().contains(ariArea + "/") || policy.getArrival().contains(ariCountryCode + "/")
//						|| policy.getArrival().contains(ariAirportCode + "/"))) {
//			return false;
//		}
		if (ObjectUtil.hasText(policy.getArrival())) {
			String ariCountrys = PolicyConstant
					.getCountrys(policy.getArrival().substring(0, policy.getArrival().length() - 1));
			if (!((ObjectUtil.hasText(ariCountryCode) && ariCountrys.contains(ariCountryCode + "/")) || policy.getArrival().contains(ariCountryCode + "/")
					|| policy.getArrival().contains(ariAirportCode + "/"))) {
				return false;
			}
		}
		if (ObjectUtil.hasText(policy.getArrivalExclude())
				&& (policy.getArrivalExclude().contains(ariCountryCode + "/"))
				|| policy.getArrivalExclude().contains(ariAirportCode + "/")) {
			return false;
		}
		return true;
	}
	
	public static boolean isDirectMatch(String departure, String depCountryCode, String arrival, String ariCountryCode,
			TripartitePolicy policy) {
		String depAirportCode = departure;
		String ariAirportCode = arrival;
//		String depArea = PolicyConstant.getArea(depCountryCode);
//		String ariArea = PolicyConstant.getArea(depCountryCode);
//		if (ObjectUtil.hasText(policy.getDeparture()) && !(policy.getDeparture().contains(depArea + "/")
//				|| policy.getDeparture().contains(depCountryCode + "/")
//				|| policy.getDeparture().contains(depAirportCode + "/"))) {
//			return false;
//		}
		if (ObjectUtil.hasText(policy.getDeparture())) {
			String depCountrys = PolicyConstant
					.getCountrys(policy.getDeparture().substring(0, policy.getDeparture().length() - 1));
			if (!((ObjectUtil.hasText(depCountrys) && depCountrys.contains(depCountryCode + "/")) || policy.getDeparture().contains(depCountryCode + "/")
					|| policy.getDeparture().contains(depAirportCode + "/"))) {
				return false;
			}
		}
		if (ObjectUtil.hasText(policy.getDepartureExclude())
				&& (policy.getDepartureExclude().contains(depCountryCode + "/")
						|| policy.getDepartureExclude().contains(depAirportCode + "/"))) {
			return false;
		}
//		if (ObjectUtil.hasText(policy.getArrival())
//				&& !(policy.getArrival().contains(ariArea + "/") || policy.getArrival().contains(ariCountryCode + "/")
//						|| policy.getArrival().contains(ariAirportCode + "/"))) {
//			return false;
//		}
		if (ObjectUtil.hasText(policy.getArrival())) {
			String ariCountrys = PolicyConstant
					.getCountrys(policy.getArrival().substring(0, policy.getArrival().length() - 1));
			if (!((ObjectUtil.hasText(ariCountryCode) && ariCountrys.contains(ariCountryCode + "/")) || policy.getArrival().contains(ariCountryCode + "/")
					|| policy.getArrival().contains(ariAirportCode + "/"))) {
				return false;
			}
		}
		if (ObjectUtil.hasText(policy.getArrivalExclude())
				&& (policy.getArrivalExclude().contains(ariCountryCode + "/"))
				|| policy.getArrivalExclude().contains(ariAirportCode + "/")) {
			return false;
		}
		return true;
	}
	
	public static boolean isDirectMatch(String departure, String depCountryCode, String arrival, String ariCountryCode,
			PrivateFare policy) {
		String depAirportCode = departure;
		String ariAirportCode = arrival;

		if (ObjectUtil.hasText(policy.getDeparture())) {
			String depCountrys = PolicyConstant
					.getCountrys(policy.getDeparture().substring(0, policy.getDeparture().length() - 1));
			if (!((ObjectUtil.hasText(depCountrys) && depCountrys.contains(depCountryCode + "/")) || policy.getDeparture().contains(depCountryCode + "/")
					|| policy.getDeparture().contains(depAirportCode + "/"))) {
				return false;
			}
		}
		if (ObjectUtil.hasText(policy.getDepartureExclude())
				&& (policy.getDepartureExclude().contains(depCountryCode + "/")
						|| policy.getDepartureExclude().contains(depAirportCode + "/"))) {
			return false;
		}
		if (ObjectUtil.hasText(policy.getArrival())) {
			String ariCountrys = PolicyConstant
					.getCountrys(policy.getArrival().substring(0, policy.getArrival().length() - 1));
			if (!((ObjectUtil.hasText(ariCountryCode) && ariCountrys.contains(ariCountryCode + "/")) || policy.getArrival().contains(ariCountryCode + "/")
					|| policy.getArrival().contains(ariAirportCode + "/"))) {
				return false;
			}
		}
		if (ObjectUtil.hasText(policy.getArrivalExclude())
				&& (policy.getArrivalExclude().contains(ariCountryCode + "/"))
				|| policy.getArrivalExclude().contains(ariAirportCode + "/")) {
			return false;
		}
		return true;
	}

	public static boolean isflightNoMatch(String airline, String flightNo, CommisionPolicy policy) {
		if (!isflightNoMatch(airline, flightNo, policy.getOutboundFlight(), policy.getInboundFlight(),
				policy.getFlightExclude(), policy.getLineExclude())) {
			return false;
		}
		return true;
	}

	public static boolean isflightNoMatch(String airline, String flightNo, PrivateFare policy) {
		if (!isflightNoMatch(airline, flightNo, policy.getOutboundFlight(), policy.getInboundFlight(),
				policy.getFlightExclude(), policy.getLineExclude())) {
			return false;
		}
		return true;
	}
	
	public static boolean isflightNoMatch(String airline, String flightNo, TripartitePolicy policy) {
		if (ObjectUtil.hasText(policy.getFlight())// 去程适用航班
				&& isflightNoMatch(policy.getFlight(), airline, flightNo)) {
			return false;
		}
		if (ObjectUtil.hasText(policy.getFlightExclude())// 不适用航班
				&& !isflightNoMatch(policy.getFlightExclude(), airline, flightNo)) {
			return false;
		}
		return true;
	}

	
	
	private static boolean isflightNoMatch(String airline, String flightNo, String outboundFlight, String inboundFlight,
			String flightExclude, String lineExclude) {
		if (ObjectUtil.hasText(outboundFlight)// 去程适用航班
				&& isflightNoMatch(outboundFlight, airline, flightNo)) {
			return false;
		}
		if (ObjectUtil.hasText(flightExclude)// 不适用航班
				&& !isflightNoMatch(flightExclude, airline, flightNo)) {
			return false;
		}
		if (ObjectUtil.hasText(inboundFlight)// 回程适用航班
				&& isflightNoMatch(inboundFlight, airline, flightNo)) {
			return false;
		}
		return true;
	}

	/**
	 * 不适用航线
	 * 
	 * @param departure
	 * @param arrival
	 * @param flightLineExclude
	 * @return
	 */
	public static boolean isflightLineMatch(String departure, String arrival, String flightLineExclude) {
		if (ObjectUtil.hasText(flightLineExclude) && flightLineExclude.contains(departure + "-" + arrival + "/")) {
			return false;
		}
		return true;
	}

	private static boolean isflightNoMatch(String policyflightNos, String airline, String flightNo) {
		String[] strings = policyflightNos.split("/");
		for (String string : strings) {
			if (string.equals(airline + flightNo)) {
				return false;
			}
			if (string.contains("*") && (airline + flightNo).contains(string.replace("*", ""))) {
				return false;
			}
			if (string.contains("-") && airline.equals(string.substring(0, 2))) {
				String[] strings2 = string.split("-");
				if (strings2.length == 2) {
					if (airline.equals(strings2[0].substring(0, 2)) && airline.equals(strings2[1].substring(0, 2))) {
						if (Integer.valueOf(strings2[0].substring(2)) < Integer.valueOf(flightNo)
								&& Integer.valueOf(strings2[1].substring(2)) > Integer.valueOf(flightNo)) {
							return false;
						}
					}
				}
			}
		}
		return true;
	}

	public static boolean isClientMatch(Long orderClientId, Long policyClientId) {
		if(orderClientId == null || orderClientId == 0 || policyClientId == null || policyClientId == 0){
			return false;
		}
		if(orderClientId != policyClientId){
			return false;
		}
		return true;
	}
	
	public static boolean isOfficeNoMatch(String policyOfficeNo, String orderOfficeNo){
		if(ObjectUtil.noText(policyOfficeNo) || ObjectUtil.noText(orderOfficeNo) ){
			return false;
		}
		if(!policyOfficeNo.equals(orderOfficeNo)){
			return false;
		}
		return true;
	}
	
	public static boolean isFlightTypeMatch(String policyFlightType, String orderFlightType){
		if(ObjectUtil.noText(policyFlightType) || ObjectUtil.noText(orderFlightType)){
			return false;
		}
		if(policyFlightType.contains(orderFlightType)){
			return true;
		}
		return false;
	}
}
